var foot_ops = {
    init: function () {
        this.eventBind();
    },
    eventBind: function () {
        $('.save').click(function (ev) {
            ev.preventDefault();

            var name_i = $('input[name=name]');
            var weight_i = $('input[name=weight]');
            var name = name_i.val();
            var weight = weight_i.val();
            var params = common_ops.getParams();
            var method = '';
            if (params.id == null || params.id.length<1) {
                method = 'add';
            }
            else {
                method = 'update';
            }

            if (common_ops.isEmpty(name)) {
                common_ops.tip('请输入分类名称', name_i);
                return;
            }
            if (common_ops.isEmpty(weight)) {
                common_ops.tip('请输入权重', weight_i);
                return;
            }
            if (!common_ops.isNumber(weight)) {
                common_ops.tip('权重只能为数字', weight_i);
                return;
            }

            $.ajax({
                url: '/food/cat-set',
                data: {
                    'id': params.id,
                    'name': name,
                    'weight': weight,
                    'method': method
                },
                type: 'POST',
                dataType: 'json',
                success: function (data) {
                    if (data['code'] === 200) {
                        common_ops.alert(method==='add'?'分类添加成功！':'分类更新成功！');
                        window.location.href = '/food/cat';
                    }
                    else {
                        common_ops.alert(data['msg']);
                    }
                },
                fail: function () {
                    common_ops.alert('数据提交失败，请重试！');
                }
            });
        });
    }
};

$(function () {
    foot_ops.init();
});